// $(function(){				//原始数据，json构建
//     $('#table').basictable({
//         breakpoint: 768
//     });
//     $("#table").reponsetable({
//         "id":"table",
//         "operation": "editer",
//         "type":"numbers",
//         "colum": [
//             {"field": "id","title": "学号"},
//             {"field": "name","title": "姓名"},
//             {"field": "date","title": "入学日期"},
//             {"field": "telnum","title": "电话"},
//             {"field": "grade","title": "学期"},
//             {"field": "sta","title": "学籍状态"},
//
//             {"field": "major","title": "专业"},
//             {"field": "sex","title": "性别"},
//             {"field": "subject","title": "科目"},
//             {"field": "score","title": "成绩"}
//         ],
//         "data": [
//             {"id": 1862801,"name": "张三","date": 20200101,"telnum": "13512345678","grade": 1,"sta": "A","major": "计算机","grade": "2018","sex": "男", "subject": "数据结构", "score": 98},
//             {"id": 1862802,"name": "张三","date": 20200102,"telnum": "13512345678","grade": 1,"sta": "A","major": "计算机","grade": "2018","sex": "男", "subject": "数据结构", "score": 98},
//             {"id": 1862803,"name": "张三","date": 20200103,"telnum": "13512345678","grade": 1,"sta": "A","major": "计算机","grade": "2018","sex": "男", "subject": "数据结构", "score": 98}
//         ]
//     });
//
//     $('#search_table').basictable({
//         breakpoint: 768
//     });
//     $("#search_table").reponsetable({
//         "id":"search_table",
//         "operation": "editer",
//         "type":"numbers",
//         "colum": [
//             {"field": "id","title": "学号"},
//             {"field": "name","title": "姓名"},
//             {"field": "date","title": "入学日期"},
//             {"field": "telnum","title": "电话"},
//             {"field": "grade","title": "学期"},
//             {"field": "sta","title": "学籍状态"},
//
//             {"field": "major","title": "专业"},
//             {"field": "sex","title": "性别"},
//             {"field": "subject","title": "科目"},
//             {"field": "score","title": "成绩"}
//         ],
//         "data": []
//     });
// })

function checkId() {//检查学号是否合适
    var id = $("#input_id").val()
    console.log("aaa")//成功
    // var pattern=/^\d{7}$/;
    if(id == "" || id == null) {
        $("#idNumWarn").html("学号不能为空！");
        return false;
    } else if (!checkIdReg(id)) {
        $("#idNumWarn").html("学号必须为7为数字！");
        return false;
    } else {
        $("#idNumWarn").html("");
        return true;
    }
}
function checkdate() {//检查学号是否合适  /
    var date = $("#input_date").val()
    console.log("b")
    // var pattern=/^2020(?:(?:0[1-9])|(?:1[0-2]))(?:(?:[0-2][1-9])|(?:[1-3][0-1]))$/;
    if(date == "" || date == null) {
        $("#dataWarn").html("日期不能为空！");
        return false;
    } else if (!checkdate(date)) {
        $("#dataWarn").html("日期必须为8为数字！");
        return false;
    } else {
        $("#dataWarn").html("");
        return true;
    }
}

function checkName() {//检查姓名是否合适
    var name = $("#input_name").val()
    console.log("c")
    if(name == "" || name == null) {
        $("#nameWarn").html("姓名不能为空！")
        return false;
    } else if (!checkNameReg(name)) {
        $("#nameWarn").html("姓名必须为2-4个汉字")
        return false;
    } else {
        $("#nameWarn").html("")
        return true
    }
}

function checkScore() {//检查分数是否合适
    var score = $("#input_score").val()
    console.log("d")
    if(score == "" || score == null) {
        $("#scoreWarn").html("分数不能为空！")
        return false;
    } else if (!checkScoreReg(score)) {
        $("#scoreWarn").html("分数必须是0-100！")
        return false;
    } else {
        $("#scoreWarn").html("")
        return true
    }
}

function checkScore() {//检查电话号码是否合适
    var num = $("#input_score").val()
    console.log("e")
    if(num == "" || num == null) {
        $("#numcheck").html("电话号码不能为空！")
        return false;
    } else if (!checkNum(num)) {
        $("#numcheck").html("")
        return false;
    } else {
        $("#numcheck").html("")
        return true
    }
}

function checkIdReg(theObj) {//正则判断学号是否合格
    var reg = /^\d{7}$/;
    if (reg.test(theObj)) {
        return true;
    }
    return false;
}

function checkdate(theObj) {//正则判断日期是否合格
    var pattern=/^2020(?:(?:0[1-9])|(?:1[0-2]))(?:(?:[0-2][1-9])|(?:[1-3][0-1]))$/;
    if (pattern.test(theObj)) {
        return true;
    }
    return false;
}
function checkNameReg(theObj) {//正则判断姓名是否合格
    var reg = /^[\u4e00-\u9fa5]{2,4}$/;
    if (reg.test(theObj)) {
        return true;
    }
    return false;
}

function checkNum(theObj) {//正则判断分数是否合格
    var reg = /^1[3|4|5|7|8][0-9]{9}$/;
    if (reg.test(theObj)) {
            return true;
        }
    return false;
}

function checkSearchScoreReg(theObj) {//正则判断分数范围是否合格
    var reg = /^[0-9]+.?[0-9]*$/;
    if ((theObj.indexOf("-") > -1) && ((theObj.split('-')).length-1 == 1)) {
        var scores = theObj.split('-');
        console.log(scores)
        if (parseInt(scores[0]) > (parseInt(scores[1]))) {
            return false;
        } else {
            if (reg.test(scores[0])) {
                if ((parseInt(scores[0]) <= 100) && (parseInt(scores[0]) >= 0)) {
                    if (reg.test(scores[1])) {
                        if ((parseInt(scores[1]) <= 100) && (parseInt(scores[1]) >= 0)) {
                            return true;
                        }
                    }
                }
            }
        }

    }

    return false;
}

function submitInfo() {//提交表单信息，表中有的所有信息的判定如上方法1
    if ((!checkId()) || (!checkName()) || (!checkScore())||(!checkNum())||(!checkdate())) {
        return false
    }
    var major;
    var majorValue = $('input:radio[name="radio_major"]:checked').val()

    if (majorValue==1) {
        major = "计算机";
    } else if (majorValue==2) {
        major = "物联网";
    } else {
        major = "通信";
    };

    var grade = $("#select_grade option:selected").text()
    var id = $("#input_id").val()
    var name = $("#input_name").val()
    var date = $("#input_date").val()
    var telnum = $("#input_number").val()
    var sta;
    var statuValue = $('input:radio[name="radio_s"]:checked').val()
    if (statuValue==1) {
        sta = "A";
    } else {
        sta = "L";
    };
    var sex;
    var sexValue = $('input:radio[name="radio_sex"]:checked').val()
    if (sexValue==1) {
        sex = "男";
    } else {
        sex = "女";
    };

    var subject;
    var subjectValue = $('input:radio[name="radio_subject"]:checked').val()
    if (subjectValue==1) {
        subject = "数据结构";
    } else if (subjectValue==2) {
        subject = "c语言程序设计";
    } else {
        subject = "java语言程序设计";
    }

    var score = $("#input_score").val()

    var obj = {
        "major":major,
        "grade": grade,
        "id": id,
        "name": name,
        "date":date,
        "telnum":telnum,
        "sta":sta,
        "sex": sex,
        "subject":subject,
        "score":score
    };

    var idSubject = getData();
    var inputIdSubject = id + "_" + subject;
    if (idSubject.indexOf(inputIdSubject)>=0) {
        alert("学号和科目重复，请重新填写！")
        return false;
    } else {
        reponse.addtr(obj, "table");
        return false;
    }
    console.log("f")
    return false;
}

function getData() {		//获取所有的学号+科目的信息，并形成一个数组返回
    var ids = reponse.Columndata("id", "table");
    var subjects = reponse.Columndata("subject", "table")
    var obj = []
    for (var i = 0; i < ids.length; i++) {
        obj.push(ids[i] + "_" + subjects[i])
    };
    console.log(obj);
    return obj;
}

function edittr(a, e) {//修改 ：将原来新重新写入表单
    //转换修改按钮和确认提交表单按钮，一个表示修改信息，一个表示提交表单
    console.log("gg")
    document.getElementById("btn_edit").style.display = "block";
    document.getElementById("btn_add").style.display = "none";
    console.log("as")
    var tr = $(a).parent().parent();
    reponse.resiverowdata(tr, "table");
    var rowdata = $("#table").data("rowdata");
    //alert(JSON.stringify(rowdata));

    var edit_major = rowdata["major"];
    if (edit_major == "计算机") {
        $("#radio_computer").attr("checked", "checked");
        $("#radio_iot").removeAttr("checked");
        $("#radio_tongxin").removeAttr("checked");
    } else if (edit_major == "物联网") {
        $("#radio_iot").attr("checked", "checked");
        $("#radio_computer").removeAttr("checked");
        $("#radio_tongxin").removeAttr("checked");
    } else if (edit_major == "通信") {
        $("#radio_tongxin").attr("checked", "checked");
        $("#radio_computer").removeAttr("checked");
        $("#radio_iot").removeAttr("checked");
    }
    ;

    var edit_grade = rowdata["grade"];
    $("#select_grade option:selected").text(edit_grade);

    var edit_id = rowdata["id"];
    $('#input_id').attr('value', edit_id);

    var edit_name = rowdata["name"];
    $('#input_name').attr('value', edit_name);

    var edit_date = rowdata["date"];
    $('#input_date').attr('value', edit_date);

    var edit_telnum = rowdata["telnum"];
    $('#input_number').attr('value', edit_telnum);

    var edit_sta = rowdata["sta"];
    if (edit_sta == "A") {
        $("#radio_A").attr("checked", "checked");
        $("#radio_L").removeAttr("checked");
    } else {
        $("#radio_L").attr("checked", "checked");
        $("#radio_A").removeAttr("checked");
    }

    var edit_sex = rowdata["sex"];
    if (edit_sex == "男") {
        $("#radio_man").attr("checked", "checked");
        $("#radio_women").removeAttr("checked");
    } else {
        $("#radio_women").attr("checked", "checked");
        $("#radio_man").removeAttr("checked");
    }

    var edit_subject = rowdata["subject"];
    if (edit_major == "数据结构") {
        $("#radio_ds").attr("checked", "checked");
        $("#radio_java").removeAttr("checked");
        $("#radio_c").removeAttr("checked");
    } else if (edit_major == "c语言程序设计") {
        $("#radio_c").attr("checked", "checked");
        $("#radio_ds").removeAttr("checked");
        $("#radio_java").removeAttr("checked");
    } else if (edit_major == "java语言程序设计") {
        $("#radio_java").attr("checked", "checked");
        $("#radio_ds").removeAttr("checked");
        $("#radio_c").removeAttr("checked");
    }


    var edit_score = rowdata["score"];
    $('#input_score').attr('value', edit_score);
}
    function editInfo() {//编辑信息，只修改信息，不会新增一行到表中
        var major;
        var majorValue = $('input:radio[name="radio_major"]:checked').val()

        if (majorValue == 1) {
            major = "计算机";
        } else if (majorValue == 2) {
            major = "物联网";
        } else {
            major = "通信";
        }
        ;

        var grade = $("#select_grade option:selected").text()
        var id = $("#input_id").val()
        var name = $("#input_name").val()
        var date = $("#input_date").val()
        var telnum = $("#input_number").val()
        var sta;
        var statuValue = $('input:radio[name="radio_s"]:checked').val()
        if (statuValue == 1) {
            sta = "A";
        } else {
            sta = "L";
        }
        ;
        var sex;
        var sexValue = $('input:radio[name="radio_sex"]:checked').val()
        if (sexValue == 1) {
            sex = "男";
        } else {
            sex = "女";
        }
        ;

        var subject;
        var subjectValue = $('input:radio[name="radio_subject"]:checked').val()
        if (subjectValue == 1) {
            subject = "数据结构";
        } else if (subjectValue == 2) {
            subject = "c语言程序设计";
        } else {
            subject = "java语言程序设计";
        }

        var score = $("#input_score").val()

        var obj = {
            "major": major,
            "grade": grade,
            "id": id,
            "name": name,
            "date": date,
            "telnum": telnum,
            "sta": sta,
            "sex": sex,
            "subject": subject,
            "score": score
        };

        if (checkId() && checkName() && checkdate() && checkNum()) {
            reponse.editsavetr(obj, "table");
        }
        ;
        //修改完成后提交按转换回来
        console.log("11")
        document.getElementById("btn_edit").style.display = "none";
        console.log("22")
        document.getElementById("btn_add").style.display = "block";
        console.log("33")
        return false;
    }

    function addNew() {//清空表单中的信息
        $('#input_id').attr('value', " ");
        $('#input_name').attr('value', " ");
        $('#input_date').attr('value', " ");
        $('#input_number').attr('value', " ");
        $('#input_score').attr('value', " ");
    }

    function checkSearch() {		//检查搜索输入框是否符合
        var input_search = $("#input_search").val()
        if (input_search == "" || input_search == null) {
            $("#searchWarn").html("查询字段不能为空！")
            return false;
        } else {
            $("#searchWarn").html("")
            return true;
        }
    }

    function checkSearchScore() {		//检查成绩输入格式是否合格
        var input_search = $("#input_search").val()
        if (input_search == "" || input_search == null) {
            $("#searchWarn").html("查询字段不能为空！")
            return false;
        } else if (!checkSearchScoreReg(input_search)) {
            $("#searchWarn").html("分数查询格式必须为：xx-xx 且左小右大！")
            return false;
        } else {
            $("#searchWarn").html("")
            return true
        }
    }

    function getTableData() {		//获取表格中的数据
        var ids = reponse.Columndata("id", "table");
        var names = reponse.Columndata("name", "table");
        var date = reponse.Columndata("date", "table");
        var telnum = reponse.Columndata("telnum", "table");
        var grades = reponse.Columndata("grade", "table");
        var sta = reponse.Columndata("sta", "table");
        var majors = reponse.Columndata("major", "table");
        var sexs = reponse.Columndata("sex", "table");
        var subjects = reponse.Columndata("subject", "table");
        var scores = reponse.Columndata("score", "table");
        var obj = []
        for (var i = 0; i < ids.length; i++) {
            var obj_json = {
                "id": ids[i],
                "name": names[i],
                "date": date[i],
                "telnum": telnum[i],
                "sta": sta[i],
                "grade": grades[i],
                "major": majors[i],
                "sex": sexs[i],
                "subject": subjects[i],
                "score": scores[i]
            };
            obj.push(obj_json)
        }
        ;
        return obj;		//获取表中所有的数据				//获取表中所有的数据
    }

    function searchById() {	//根据学号和姓名查询
        $("#search_table  tr:not(:first)").html("");
        if (!checkSearch()) {
            return false;
        }
        var input_search = $("#input_search").val();
        var names = reponse.Columndata("name", "table");
        var ids = reponse.Columndata("id", "table");
        var res = getTableData();
        if (ids.indexOf(input_search) >= 0) {
            for (var i = 0; i < res.length; i++) {
                if (res[i]["id"] != input_search) {
                    delete res[i];
                }
            }
        } else if (names.indexOf(input_search) >= 0) {
            for (var i = 0; i < res.length; i++) {
                if (res[i]["name"] != input_search) {
                    delete res[i];
                }
            }
        } else {
            alert("无此记录")
            return false
        }
        res = $.grep(res, function (i) {
            return i
        });
        console.log(res)

        $.grep(res, function (i) {
            reponse.addtr(i, "search_table");
        });
        return true;
    }

    function searchBySubject() {	//根据科目查询
        $("#search_table  tr:not(:first)").html("");
        if (!checkSearch()) {
            return false;
        }
        var input_search = $("#input_search").val();
        var subjects = reponse.Columndata("subject", "table");
        var res = getTableData();
        console.log(res, subjects)
        if (subjects.indexOf(input_search) >= 0) {
            for (var i = 0; i < res.length; i++) {
                if (res[i]["subject"] != input_search) {
                    delete res[i];
                }
            }
        } else {
            alert("无此记录");
            return false;
        }
        res = $.grep(res, function (i) {
            return i
        });

        $.grep(res, function (i) {
            reponse.addtr(i, "search_table");
        });
        return true;
    }

    function searchByScore() {
        $("#search_table  tr:not(:first)").html("");
        if (!checkSearchScore()) {
            return false;
        }
        var input_search = $("#input_search").val()
        var scores = reponse.Columndata("score", "table");
        var input_score = input_search.split("-");

        var res = getTableData();
        // console.log(res, input_score, scores)
        scores = $.grep(scores, function (i) {
            if (parseInt(input_score[1]) >= parseInt(i) && parseInt(input_score[0]) <= parseInt(i))
                return i
        });
        // console.log(scores)
        if (scores.length != 0) {
            for (var i = 0; i < res.length; i++) {
                if ((parseInt(res[i]["score"]) > parseInt(input_score[1])) || (parseInt(res[i]["score"]) < parseInt(input_score[0]))) {
                    delete res[i];
                }
            }
        } else {
            alert("无此记录");
            return false;
        }
        res = $.grep(res, function (i) {
            return i
        });
        console.log(res)
        $.grep(res, function (i) {
            reponse.addtr(i, "search_table");
        });
        return true;
    }

    function uptr(btn, e) {//改行上移
        var tr = $(btn).parent().parent();
        reponse.moveuptr(tr, "table");
    }

    function downtr(btn, e) {//改行下移
        var tr = $(btn).parent().parent();
        reponse.moveDown(tr, "table");
    }

    function deletetr(a, e) {//删除改行信息
        if (window.confirm('确定删除吗？')) {
            var tr = $(a).parent().parent();
            reponse.deletetr(tr, e);
            return true;
        } else {
            return false;
        }
    }
